{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6b87e1d9-ddc3-435a-a0b5-8451e9d37e59",
   "metadata": {},
   "source": [
    "Chapter 19\n",
    "# 从网页下载数据\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "76940719-9e1c-4863-a2c8-7dd76cd98c5e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7c8d2974-012a-4ce4-baca-c5cc9b700cd8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 设置数据源的URL\n",
    "url = 'https://fred.stlouisfed.org/data/SP500.txt'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d660d99e-f0e6-480c-b8d7-aef10c98734d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 发送GET请求并获取数据\n",
    "response = requests.get(url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "efa6125f-78c2-4afd-8ff0-e5a2aba9fe6d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 检查是否成功获取数据\n",
    "if response.status_code == 200:\n",
    "    # 数据以制表符分隔\n",
    "    df = pd.read_csv(url,skiprows=44, sep='\\s+')\n",
    "else:\n",
    "    print(\"Failed to fetch data from the source\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "268d4921-bb77-4927-aa2d-303e0a934656",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['DATE'] = pd.to_datetime(df['DATE'])\n",
    "df.set_index('DATE', inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6436fdee-7ab8-4b53-a0fd-43baf7ef5e53",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>VALUE</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>DATE</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2013-10-03</th>\n",
       "      <td>1678.66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-10-04</th>\n",
       "      <td>1690.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-10-07</th>\n",
       "      <td>1676.12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-10-08</th>\n",
       "      <td>1655.45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-10-09</th>\n",
       "      <td>1656.40</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              VALUE\n",
       "DATE               \n",
       "2013-10-03  1678.66\n",
       "2013-10-04  1690.50\n",
       "2013-10-07  1676.12\n",
       "2013-10-08  1655.45\n",
       "2013-10-09  1656.40"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
